package org.kalai.unifypay.util;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.kalai.unifypay.Constant;

public final class DBUtil {
	
	private static final BasicDataSource DATA_SOURCE;
	
	static{
		DATA_SOURCE = new BasicDataSource();
		DATA_SOURCE.setDriverClassName(Constant.JDBC_DRIVER);
		DATA_SOURCE.setUrl(Constant.JDBC_URL);
		DATA_SOURCE.setUsername(Constant.JDBC_USERNAME);
		DATA_SOURCE.setPassword(Constant.JDBC_PASSWORD);
		DATA_SOURCE.setInitialSize(10);
	}
	
	public static DataSource getDataSource(){
		return DATA_SOURCE;
	}

	public static Connection getConnection(){
		try {
			return DATA_SOURCE.getConnection();//从连接池中获取连接
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} 
	}
}